package cn.qingyun.gis.modules.youshang.controller;

import cn.hutool.core.date.DateUtil;
import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import cn.qingyun.gis.modules.youshang.entity.YsChannelProofread;
import cn.qingyun.gis.modules.youshang.entity.dto.ExportChannelRecordDto;
import cn.qingyun.gis.modules.youshang.request.YsChannelRequest;
import cn.qingyun.gis.modules.youshang.request.YsProofreadRequest;
import cn.qingyun.gis.modules.youshang.request.YsQueryRequest;
import cn.qingyun.gis.modules.youshang.service.IYsChannelProofreadService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 * @Description: 友商渠道勘误表后台管理
 * @Version: V1.0
 */
@Api(tags = "友商渠道勘误")
@RestController
@RequestMapping("/app/youshang/ys-channel-proofread")
@Slf4j
public class YsChannelProofreadController extends BasicController<YsChannelProofread, IYsChannelProofreadService> {
    @Resource
    private IYsChannelProofreadService iYsChannelProofreadService;

    // 前端操作
    @ApiOperation(value = "初始化友商渠道勘误数据csv文件")
    @PostMapping("/importYsChannelCsv0912")
    public Result<?> importYsChannelCsv0921(@RequestParam("file") MultipartFile file) {
        iYsChannelProofreadService.importYsChannelCsv0912(file);
        return Result.okData("正在导入友商渠道勘误数据.csv, 请稍后");
    }

    @ApiOperation(value = "导出友商渠道勘误数据csv文件")
    @GetMapping("/exportYsChannelCsv0912")
    public void exportYsChannelCsv0912() {
        iYsChannelProofreadService.exportYsChannelCsv0912();
    }

    @ApiOperation(value = "格式化友商渠道勘误数据csv文件")
    @GetMapping("/formattingYsChannelCsv0912")
    public Result<?> formattingYsChannelCsv0912() {
        iYsChannelProofreadService.formattingYsChannelCsv0912();
        return Result.okData("正在格式化友商渠道勘误数据.csv, 请稍后");
    }

    @ApiOperation(value = "导出每日友商渠道勘误数据增量明细-在线下载")
    @GetMapping("/exportYsChannelDetailOnline")
    public void exportYsChannelDetailOnline() {
        String fileName = "ExportYsChannelDetail" + DateUtil.format(DateUtil.date(), "yyyyMMdd") + ".xlsx";
        iYsChannelProofreadService.exportYsChannelTotal(null, fileName, "online");
    }

    @ApiOperation(value = "导出友商渠道勘误数据全量明细 + 汇总记录-在线下载")
    @GetMapping("/exportYsChannelTotal")
    public void exportYsChannelExcel(HttpServletRequest request) {
        iYsChannelProofreadService.exportYsChannelExcel(request);
    }

    @ApiOperation(value = "根据权限字段导出友商全量门头照片")
    @GetMapping("/exportYsPhotos")
    public void exportYsPhotos() {
        iYsChannelProofreadService.exportYsPhotos();
    }

    @ApiOperation(value = "展示友商渠道勘误数据汇总列表")
    @GetMapping("/analysisDataList")
    public Result<?> analysisDataList(HttpServletRequest request, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<ExportChannelRecordDto> page = new Page<>(pageNo, pageSize);
        return Result.okData(iYsChannelProofreadService.analysisDataList(request, page));
    }

    @GetMapping("/query")
    @ApiOperation("友商渠道未勘误列表查询")
    public Result<?> selectList(@Validated YsQueryRequest request) {
        return Result.okData(iYsChannelProofreadService.queryList(request));
    }

    @PostMapping("/ysProofread")
    @ApiOperation("友商渠道勘误")
    public Result<?> ysProofread(@RequestBody YsProofreadRequest request) {
        return Result.ok(iYsChannelProofreadService.ysProofread(request));
    }

    @GetMapping("/queryTotal")
    @ApiOperation("友商渠道勘误列表查询")
    public Result<?> queryTotal(@Validated YsQueryRequest request) {
        return Result.okData(iYsChannelProofreadService.queryTotal(request));
    }

    @GetMapping("/matchGrid")
    @ApiOperation("友商渠道-匹配业务规划")
    public Result<?> matchGrid() {
        iYsChannelProofreadService.matchGrid();
        return Result.okData("正在进行友商渠道的业务区域匹配");
    }

    // 后端管理

    /**
     * 分页列表查询
     *
     * @param ysChannelProofread
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "友商渠道勘误表-分页列表查询")
    @ApiOperation(value = "友商渠道勘误表-分页列表查询", notes = "友商渠道勘误表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YsChannelProofread ysChannelProofread,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<YsChannelProofread> queryWrapper = QueryGenerator.initQueryWrapper(ysChannelProofread, req.getParameterMap());
        queryWrapper.lambda().orderByDesc(YsChannelProofread::getUpdateTime);
        Page<YsChannelProofread> page = new Page<>(pageNo, pageSize);
        IPage<YsChannelProofread> pageList = iYsChannelProofreadService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    /**
     * 添加
     *
     * @param ysChannelProofread
     * @return
     */
    @AutoLog(value = "友商渠道勘误表-添加")
    @ApiOperation(value = "友商渠道勘误表-添加", notes = "友商渠道勘误表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YsChannelProofread ysChannelProofread) {
        iYsChannelProofreadService.save(ysChannelProofread);
        return Result.ok("添加成功！");
    }

    /**
     * 友商渠道新增
     *
     * @param ysChannelRequest
     * @return
     */
    @AutoLog(value = "友商渠道-添加")
    @ApiOperation(value = "友商渠道-添加", notes = "友商渠道-添加")
    @PostMapping(value = "/addYsChannel")
    public Result<?> addYsChannel(@RequestBody YsChannelRequest ysChannelRequest) {
        iYsChannelProofreadService.addYsChannel(ysChannelRequest);
        return Result.ok("添加渠道成功！");
    }

    /**
     * 编辑
     *
     * @param ysChannelProofread
     * @return
     */
    @AutoLog(value = "友商渠道勘误表-编辑")
    @ApiOperation(value = "友商渠道勘误表-编辑", notes = "友商渠道勘误表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody YsChannelProofread ysChannelProofread) {
        ysChannelProofread.setUpdateTime(DateUtil.date());
        iYsChannelProofreadService.updateById(ysChannelProofread);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "友商渠道勘误表-通过id删除")
    @ApiOperation(value = "友商渠道勘误表-通过id删除", notes = "友商渠道勘误表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        iYsChannelProofreadService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "友商渠道勘误表-批量删除")
    @ApiOperation(value = "友商渠道勘误表-批量删除", notes = "友商渠道勘误表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.iYsChannelProofreadService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "友商渠道勘误表-通过id查询")
    @ApiOperation(value = "友商渠道勘误表-通过id查询", notes = "友商渠道勘误表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        YsChannelProofread ysChannelProofread = iYsChannelProofreadService.getById(id);
        if (ysChannelProofread == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(ysChannelProofread);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ysChannelProofread
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YsChannelProofread ysChannelProofread) {
        return super.exportXls(request, ysChannelProofread, YsChannelProofread.class, "友商渠道勘误表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YsChannelProofread.class);
    }

    @PostMapping("/delete/poiId")
    public Result<?> deleteByPoiId(@RequestParam("file") MultipartFile file) {
        int i = iYsChannelProofreadService.deleteByPoiId(file);
        return Result.okData(String.format("删除成功%d条数据", i));
    }

    @PostMapping("/update/poiId")
    public Result<?> updateByPoiId(@RequestParam("file") MultipartFile file) {
        int i = iYsChannelProofreadService.updateByPoiId(file);
        return Result.okData(String.format("更新成功%d条数据", i));
    }
}
